<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-28
 * Time: 21:58
 */

namespace app\api\controller;

use app\common\BaseLoginCustomer;
use app\exception\CustomException;
use think\Exception;

class Express extends BaseLoginCustomer
{

    // 查询记录
    function log()
    {

        $customer_id = $this->getParam('u_id', 0);
        $page = $this->getParam('page', 1);
        $page_size = $this->getParam('page_size', 10);

        $where = [
            "l.uid"=> $customer_id
        ];

        $list = db('search_log l')
            ->join('express_log el', 'l.log_id = el.id')
            ->join('express e', 'el.express_id = e.id')
            ->where($where)
            ->field("e.express, e.type, el.express_no, el.detail, l.time, l.name, e.logo")
            ->page($page, $page_size)
            ->order("l.id desc")
            ->select();

        foreach ($list as &$v){
            $v['time'] = date('Y-m-d H:i', strtotime($v['time']));
            $v['detail'] = json_decode($v['detail'], true);
        }

        return success($list);

    }

    // 查询

    function search()
    {
        $express_no = $this->getParam('express_no', '');
        $type = $this->getParam('type', 'auto');
        $uid = $this->getParam('u_id', 0);

        if(!$express_no || !$type)
        {
            return fail("DATA_ERROR");
        }
        $express = db("express")->where(['type'=> $type])->field('id, logo')->find();

        $data = db('express_log l')
            ->join("express e", "e.id = l.express_id")
            ->where(['l.express_no'=> $express_no, 'l.status'=> 1, 'l.express_id'=> $express['id']])
            ->field('l.express_no, l.detail, e.express, e.type, l.id, l.status, e.logo')
            ->find();

        if(!$data)
        {
            $ex = new \app\tools\Express();
            $data = $ex->search($express_no, $type);

            $post = [
                "express_id"=> $express['id'],
                "express_no"=> $data['express_no'],
                "status"=> $data['status'],
                "detail"=> json_encode($data['detail'], JSON_UNESCAPED_UNICODE),
            ];

            $id = db('express_log')->where(['express_no'=> $express_no, 'express_id'=> $express['id']])->value('id');
            if($id)
            {
                db('express_log')->where(['id'=> $id])->update($post);
            }else{
                $id = db('express_log')->insertGetId($post);
            }

            $data['id'] = $id;
            $data['logo'] = $express['logo'];

        }else{
            $data['detail'] = json_decode($data['detail'], true);
            $id = $data['id'];
        }

        $search_id = db("search_log")->where(['uid'=> $uid, "log_id"=> $id])->value('id');

        if(!$search_id)
        {
            db("search_log")->insert([
                "log_id"=> $id,
                "uid"=> $uid,
            ]);
        }else{
            db('search_log')->where(['id'=> $search_id])->update([
                "time"=> date("Y-m-d H:i:s")
            ]);
        }


        return success($data);

    }

    function company()
    {

        $data = db("express")->select();

        return success($data);

    }

}